﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.Security;
using System.Web.Security;
using System.Configuration;
using RentalSystem.Data.SQLServer;
using RentalSystem.Data.EF.Core;
using Web.Models;
using Web.Helper;
using System.Web.Mvc;

namespace Web.Context
{
    public class CurrentContext
    {
        private IDataContext dataContext;
        private SecurityManager securityManager;
        private LogsManager logsManager;
        private CurrentCache currentCache;
        private SettingsManager settingsManager;

        public static CurrentContext Context
        {
            get
            {
                CurrentContext context = HttpContext.Current.Items["Context"] as CurrentContext;

                if (context == null)
                {
                    context = new CurrentContext();
                    HttpContext.Current.Items["Context"] = context;
                }

                return context;
            }
        }

        public User CurrentUser
        {
            get
            {
                User user = HttpContext.Current.Session["CurrentUser"] as User;

                if (user == null)
                {
                    MembershipUser membershipUser = Membership.GetUser();

                    if (membershipUser != null)
                    {
                        user = new User(DataContext.UsersDao.GetByEmail(membershipUser.Email));
                    }
                }

                return user;
            }
            set
            {
                HttpContext.Current.Session["CurrentUser"] = value;
            }
        }

        public IDataContext DataContext
        {
            get
            {
                if (this.dataContext == null)
                {
                    this.dataContext = new EFDataContext();
                }

                return this.dataContext;
            }
        }

        public SecurityManager SecurityManager
        {
            get
            {
                if (this.securityManager == null)
                {
                    this.securityManager = new SecurityManager();
                }

                return this.securityManager;
            }
        }

        public LogsManager LogsManager
        {
            get
            {
                if (this.logsManager == null)
                {
                    this.logsManager = new LogsManager();
                }

                return this.logsManager;
            }
        }

        public SettingsManager SettingsManager
        {
            get
            {
                if (this.settingsManager == null)
                {
                    this.settingsManager = new SettingsManager();
                }

                return this.settingsManager;
            }
        }

        public CurrentCache CurrentCache
        {
            get
            {
                if (this.currentCache == null)
                {
                    this.currentCache = new CurrentCache();
                }

                return this.currentCache;
            }
        }
    }
}